layui.use(['form', 'layer','formSelects'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery;

    //var formSelects = layui.formSelects;

    form.on('submit(addOrUpdateUser)',function (data) {
        var index= top.layer.msg("数据提交中,请稍后...",{
            icon:16,
            time:false,
            shade:0.8
        });
        //得到所有的表单元素的值
        var formData = data.field;


        var url = ctx+"/user/add";

        //判断用户ID是否为空
        if($("[name='id']").val()){
           var  url=ctx+"/user/update";
        }
        $.post(url,formData,function (result) {
            if(result.code==200){
                top.layer.msg("操作成功");
                top.layer.close(index);
                layer.closeAll("iframe");
                // 刷新父页面
                parent.location.reload();
            }else{
                layer.msg(result.msg);
            }
        });
        return false;
    });



    /**
     * 关闭弹出层
     */
    $("#closeBtn").click(function () {
        //当在iframe页面关闭自身时
        var index = parent.layer.getFrameIndex(window.name);//先得到当前iframe层的索引
        parent.layer.close(index);  //再执行关闭
    });



    var  formSelects = layui.formSelects;
    var userId=$("input[name='id']").val();
    formSelects.config('selectId',{
        type:"post",
        searchUrl:ctx+"/role/queryAllRoles?userId="+userId,
        //自定义返回数据中name的key, 默认 name
        keyName: 'roleName',
        //自定义返回数据中value的key, 默认 value
        keyVal: 'id'
    },true);





});